package org.kim.command;

import org.kim.core.Session;
import org.kim.pojo.Account;
import org.kim.pojo.Book;
import org.kim.service.BookService;
import org.kim.utils.Constants;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

public class BookCommand {

    BookService bookService = new BookService();

    @Login(required = true)
    @Permission(role = "admin")
    public String addBook(String[] commandStrs) throws Exception {
        Book book = new Book();
        book.setId(UUID.randomUUID().toString().substring(0, 8));
        book.setName(commandStrs[1]);
        book.setAuthor(commandStrs[2]);
        book.setInventory(Integer.valueOf(commandStrs[3]));
        Map<String, Book> result = bookService.addBook(book);
        if (result.containsKey("add")) {
            Book book2 = (Book) result.get("add");
            return String.format(Constants.addBookSuccess, book2.getName(), book2.getAuthor(), book2.getInventory());
        } else {
            Book book2 = (Book) result.get("update");
            return String.format(Constants.updateBookInventorySuccess, book2.getName(), book2.getInventory());
        }
    }

    @Login(required = true)
    public String listBook(String[] commandStrs) throws Exception {
        List<Book> list = (ArrayList<Book>) bookService.bookLists();
        return "Book List:\n" +
                list.stream()
                        .map(e -> String.format("%s - %s - Inventory: %s", e.getName(), e.getAuthor(), e.getInventory()))
                        .collect(Collectors.joining("\n"));
    }

    @Login(required = true)
    public String borrowBook(String[] commandStrs) throws Exception {

//        $ borrow "Clean Code" " Robert C. Martin"
//        Book "Clean Code" successfully borrowed.
        Account account= Session.getAny();
        String loginId=account.getAccountId();
        Account loginAccount = new Account();
        loginAccount.setAccountId(loginId);
        Book book = new Book();
        book.setName(commandStrs[1]);
        book.setAuthor(commandStrs[2]);
        if (bookService.borrowBook(book, loginAccount)) {
            return String.format(Constants.borrowBookSuccess, book.getName());
        } else {
            return String.format(Constants.borrowBookFail, book.getName());
        }
    }

    @Login(required = true)
    public String returnBook(String[] commandStrs) throws Exception {
//        return "Clean Code" " Robert C. Martin"
//        Book "Clean Code" successfully returned.
        Account account= Session.getAny();
        String loginId=account.getAccountId();
        Account loginAccount = new Account();
        loginAccount.setAccountId(loginId);
        Book book = new Book();
        book.setName(commandStrs[1]);
        book.setAuthor(commandStrs[2]);

        if (bookService.returnBook(book, loginAccount)) {
            return String.format(Constants.returnBookSuccess, book.getName());
        } else {
            return String.format(Constants.borrowBookFail, book.getName());
        }
    }

    @Login(required = true)
    @Permission(role = "admin")
    public String deleteBook(String[] commandStrs) throws Exception {
//        delete "Clean Code" " Robert C. Martin"
//        Cannot delete book "Clean Code" because it is currently borrowed.
        Book book = new Book();
        book.setName(commandStrs[1]);
        book.setAuthor(commandStrs[2]);

        if (bookService.bookIsBorrowed(book)) {
            return String.format(Constants.bookIsBorrowed, book.getName());
        } else {
            if (bookService.deleteBook(book)) {
                return String.format(Constants.deleteBookFail, book.getName());
            } else {
                return String.format(Constants.deleteBookFail, book.getName());
            }
        }
    }

    @Login(required = true)
    public String searchBook(String[] commandStrs) throws Exception {
        String bookName = commandStrs[1];
        String author = commandStrs[2];
        List<Book> list = bookService.searchBook(bookName, author);
        return "Book search result:\n" +
                list.stream()
                        .map(e -> String.format("%s - %s - Inventory: %s", e.getName(), e.getAuthor(), e.getInventory()))
                        .collect(Collectors.joining("\n"));

    }
}
